package com.QK.cnstudy.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.QK.cnstudy.R;
import com.QK.cnstudy.SubjectActivity;

public class AnswerAdaper extends QKAdapter {

    public final static int FLAG_TITLE = 0;
    public final static int FLAG_ANSWER = 1;
    public final static int FLAG_NAVI = 2;

    int titleResource = R.layout.subject_title_item;
    int resource = R.layout.answer_item;
    int naviResource = R.layout.subject_navi;

    public Handler handler;

    @Override
    public boolean isEnabled(int position) {
        int flag = (Integer) list.get(position).get("flag");
        if (flag == FLAG_ANSWER) {
            return true;
        }
        return false;
    }

    public AnswerAdaper(Context context, List<Map<String, Object>> list, Handler handler) {
        super(context, list);
        this.handler = handler;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        Map<String, Object> map = list.get(position);
        String title = (String) map.get("title");
        ViewHolder holder = null;
        switch ((Integer) map.get("flag")) {
        case FLAG_TITLE:
            if (convertView == null || ((ViewHolder) convertView.getTag()).flag != FLAG_TITLE) {
                holder = new ViewHolder();
                convertView = inflater.inflate(titleResource, null);
                holder.textView = (TextView) convertView.findViewById(R.id.title);
                holder.flag = FLAG_TITLE;
                convertView.setTag(holder);
            }
            holder = (ViewHolder) convertView.getTag();
            holder.textView.setText(title);
            break;
        case FLAG_ANSWER:
            if (convertView == null || ((ViewHolder) convertView.getTag()).flag != FLAG_ANSWER) {
                holder = new ViewHolder();
                convertView = inflater.inflate(resource, null);
                holder.textView = (TextView) convertView.findViewById(R.id.answer);
                holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
                holder.flag = FLAG_ANSWER;
                convertView.setTag(holder);
            }
            holder = (ViewHolder) convertView.getTag();
            holder.textView.setText(Html.fromHtml(title));
            boolean selected = (Boolean) map.get("selected");
            boolean hasSelected = false;
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> _map = list.get(i);
                if ((Integer) _map.get("flag") == AnswerAdaper.FLAG_ANSWER && (Boolean) _map.get("selected") == true) {
                    hasSelected = true;
                    break;
                }
            }

            if (selected) {

                holder.layout.setBackgroundResource(R.drawable.bg_answer_select);
                // holder.textView.setTextColor(Color.RED);
            } else {
                holder.layout.setBackgroundColor(Color.TRANSPARENT);
                holder.textView.setTextColor(hasSelected != hasSelected ? Color.GRAY : Color.BLACK);
            }
            break;
        case FLAG_NAVI:
            if (convertView == null || ((ViewHolder) convertView.getTag()).flag != FLAG_NAVI) {
                holder = new ViewHolder();
                convertView = inflater.inflate(naviResource, null);
                holder.nextSubjectButton = (Button) convertView.findViewById(R.id.next_subject);
                holder.hintButton = (Button) convertView.findViewById(R.id.hint);
                holder.nextSubjectButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        handler.sendEmptyMessage(SubjectActivity.MSG_NEXTSUBJECT);
                    }
                });
                holder.hintButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        handler.sendEmptyMessage(SubjectActivity.MSG_HINT);
                    }
                });
                Boolean hasNextSubject = (Boolean) map.get("next_subject");
                if (hasNextSubject) {
                    holder.nextSubjectButton.setText("下一题");
                } else {
                    holder.nextSubjectButton.setText("提交");
                }
                holder.flag = FLAG_NAVI;
                convertView.setTag(holder);
            }
            holder = (ViewHolder) convertView.getTag();
            holder.hintButton.setText("提示");
            break;

        default:
            break;
        }
        return convertView;
    }

    class ViewHolder {
        public int flag;

        public LinearLayout layout;
        public TextView textView;
        public Button nextSubjectButton;
        public Button hintButton;
    }
}
